package com.allianz.webapps.secondController;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.druid.util.StringUtils;
import com.allianz.webapps.common.CustomerContextHolder;
import com.allianz.webapps.https.interfaces.type001.pojo.User;
import com.allianz.webapps.service.AdT2PolicyAgentsService;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

//http://localhost:8080/pad_marketing/main/coverConfirm.action
@Controller
@RequestMapping(value="/notes")
public class MyNotesController {
	
	@Autowired
	private AdT2PolicyAgentsService   policyAgentsService;
	@Autowired
	private ObjectMapper obMapper;
	
	/**
	 * //新业务照会
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="myNote_1")
	public ModelAndView myNote_1(HttpServletRequest request,HttpServletResponse response){
		ModelAndView mav=new ModelAndView();
		mav.setViewName("views/page/myNote_1");
		return mav;
	}
	/**
	 * //保全照会
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="myNote_2")
	public ModelAndView myNote_2(HttpServletRequest request,HttpServletResponse response){
		ModelAndView mav=new ModelAndView();
		mav.setViewName("views/page/myNote_2");
		return mav;
	}
	/**
	 * //保全照会详情
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="myNote_2_preservation")
	public ModelAndView myNote_2_preservation(HttpServletRequest request,HttpServletResponse response){
		ModelAndView mav=new ModelAndView();
		mav.setViewName("views/page/myNote_2_preservation");
		return mav;
	}
	/**
	 * //新单回访
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="myNote_3")
	public ModelAndView myNote_3(HttpServletRequest request,HttpServletResponse response){
		ModelAndView mav=new ModelAndView();
		mav.setViewName("views/page/myNote_3");
		return mav;
	}
	/**
	 * //新单回访详情
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="myNote_3_visit")
	public ModelAndView myNote_3_visit(HttpServletRequest request,HttpServletResponse response){
		ModelAndView mav=new ModelAndView();
		mav.setViewName("views/page/myNote_3_visit");
		return mav;
	}
	/**
	 * //理赔照会
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="myNote_4")
	public ModelAndView myNote_4(HttpServletRequest request,HttpServletResponse response){
		ModelAndView mav=new ModelAndView();
		mav.setViewName("views/page/myNote_4");
		return mav;
	};
	/**
	 * //理赔照会详情
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="myNote_4_claims")
	public ModelAndView myNote_claims(HttpServletRequest request,HttpServletResponse response){
		ModelAndView mav=new ModelAndView();
		mav.setViewName("views/page/myNote_4_claims");
		return mav;
	};
	
	@RequestMapping(value="myNote_5")//质检照会
	public ModelAndView myNote_5(HttpServletRequest request,HttpServletResponse response){
		ModelAndView mav=new ModelAndView();
		mav.setViewName("views/page/myNote_5");
		return mav;
	};
	
	 /**
	  * 新业务照会查询
	  * @param jsonParam
	  * @return
	 * @throws IOException 
	 * @throws JsonMappingException 
	 * @throws JsonParseException 
	  */
	 @SuppressWarnings("unchecked")
	@RequestMapping(value="/getMyNoteList")
	 @ResponseBody
	 public List<Map<String,Object>> getMyNotes(@RequestParam(value="jsonParam",required=false)String jsonParam,HttpSession session) throws JsonParseException, JsonMappingException, IOException{
		 String policyRef =null;
		 String phName = null;
		 String cardType = null;
		 String cardCode = null;
		 User user = (User) session.getAttribute("username");
		 long agentId = Long.parseLong(user.getAgentId());
		 if (jsonParam != null) {
			 Map<String,Object> param=obMapper.readValue(jsonParam, Map.class);
			 //保单号
			  policyRef = (String) param.get("policyRef");
			  policyRef = "".equals(policyRef)?null:policyRef;
			  //投保人
			  phName = (String) param.get("phName");
			  phName = "".equals(phName)?null:phName;
			  //证件类型
			  cardType = (String) param.get("cardType");
			  cardType = "".equals(cardType)?null:cardType;
			  //证件号码
			  cardCode = (String) param.get("cardCode");
			 cardCode = "".equals(cardCode)?null:cardCode;
		}
		 if (cardType !=null && "National ID".equals(cardCode)) {
			 cardType =null;
		}
		 CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_ALLIANZ);
		 return policyAgentsService.getPolicyAgentsList(policyRef, phName, cardType, cardCode,agentId);
	 }
	 /**
	  * 回访
	  * @param session
	  * @return
	 * @throws IOException 
	 * @throws JsonMappingException 
	 * @throws JsonParseException 
	  */
	 @RequestMapping(value="/getMyNotesVisit")
	 @ResponseBody
	 public List<Map<String,Object>>  newPolicyReturnVisit(@RequestParam(value="jsonParam",required=false)String jsonParam,HttpSession session) throws JsonParseException, JsonMappingException, IOException{
		 String policyRef =null;
		 String phName = null;
		 String cardType = null;
		 String cardCode = null;
		 User user = (User) session.getAttribute("username");
		 long agentId = Long.parseLong(user.getAgentId());
		 if (jsonParam != null) {
			 @SuppressWarnings("unchecked")
			Map<String,Object> param=obMapper.readValue(jsonParam, Map.class);
			 //保单号
			  policyRef = (String) param.get("policyRef");
			  policyRef = "".equals(policyRef)?null:policyRef;
			  //投保人
			  phName = (String) param.get("phName");
			  phName = "".equals(phName)?null:phName;
			  //证件类型
			  cardType = (String) param.get("cardType");
			  cardType = "".equals(cardType)?null:cardType;
			  //证件号码
			  cardCode = (String) param.get("cardCode");
			 cardCode = "".equals(cardCode)?null:cardCode;
		}
		 if (cardType !=null && "National ID".equals(cardCode)) {
			 cardType =null;
		}
		 CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_ALLIANZ);
		 return policyAgentsService.getPolicyReturnVisit(policyRef,phName,cardType,cardCode,agentId);
	 }
	 /**
	  * 新业务模糊查询投保人
	  * @param jsonParam
	  * @param session
	  * @return
	  * @throws JsonParseException
	  * @throws JsonMappingException
	  * @throws IOException
	  */
	@RequestMapping(value="/getFuSearch") 
	 @ResponseBody
	 public List<String> fuzzySearch(@RequestParam(value="jsonParam")String jsonParam,HttpSession session) throws JsonParseException, JsonMappingException, IOException{
		 @SuppressWarnings("unchecked")
		Map<String,Object> param = obMapper.readValue(jsonParam, Map.class);
		 String phName = (String) param.get("phName");
		 if (StringUtils.isEmpty(phName)) {
			phName = null;
		}
		 User user=(User) session.getAttribute("username");
		 long agentId=Long.parseLong(user.getAgentId());
		 CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_ALLIANZ);
		 return  policyAgentsService.getFuzzySearch(agentId, phName);
	 }
	
	/**
	 * 理赔照会查询
	 * @param session
	 * @return
	 * @throws IOException 
	 * @throws JsonMappingException 
	 * @throws JsonParseException 
	 */
	@RequestMapping(value="/getClaimList")
	@ResponseBody
	public List<Map<String,Object>> getClaimLists(@RequestParam(value="jsonParam",required=false)String jsonParam,HttpSession session) throws JsonParseException, JsonMappingException, IOException{
		 String policyRef =null;
		 String phName = null;
		 String cardType = null;
		 String cardCode = null;
		 User user = (User) session.getAttribute("username");
		 long agentId = Long.parseLong(user.getAgentId());
		 if (jsonParam != null) {
			 @SuppressWarnings("unchecked")
			Map<String,Object> param=obMapper.readValue(jsonParam, Map.class);
			 //保单号
			  policyRef = (String) param.get("policyRef");
			  policyRef = "".equals(policyRef)?null:policyRef;
			  //投保人
			  phName = (String) param.get("phName");
			  phName = "".equals(phName)?null:phName;
			  //证件类型
			  cardType = (String) param.get("cardType");
			  cardType = "".equals(cardType)?null:cardType;
			  //证件号码
			  cardCode = (String) param.get("cardCode");
			 cardCode = "".equals(cardCode)?null:cardCode;
		}
		 if (cardType !=null && "National ID".equals(cardCode)) {
			 cardType =null;
		}
		 CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_ALLIANZ);
		return policyAgentsService.getClaimNoteList(policyRef, phName, cardType, cardCode, agentId);
	}
	/**
	 * 保全照会查询
	 * @param jsonParam
	 * @param session
	 * @return
	 * @throws IOException 
	 * @throws JsonMappingException 
	 * @throws JsonParseException 
	 */
	@RequestMapping(value="/getPosList")
	@ResponseBody
	public List<Map<String,Object>>  getPosList(@RequestParam(value="jsonParam",required=false)String jsonParam,HttpSession session) throws JsonParseException, JsonMappingException, IOException{
		 String policyRef =null;
		 String phName = null;
		 String cardType = null;
		 String cardCode = null;
		User user = (User) session.getAttribute("username");
		String u = user.getAgentId();
		long uc =Long.parseLong(u);
		@SuppressWarnings("unchecked")
		Map<String,Object> param=obMapper.readValue(jsonParam, Map.class);
		 //保单号
		  policyRef = (String) param.get("policyRef");
		  policyRef = "".equals(policyRef)?null:policyRef;
		  //投保人
		  phName = (String) param.get("phName");
		  phName = "".equals(phName)?null:phName;
		  //证件类型
		  cardType = (String) param.get("cardType");
		  cardType = "".equals(cardType)?null:cardType;
		  //证件号码
		  cardCode = (String) param.get("cardCode");
		 cardCode = "".equals(cardCode)?null:cardCode;
	 if (cardType !=null && "National ID".equals(cardCode)) {
		 cardType =null;
	}
	 CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_ALLIANZ);
	return policyAgentsService.getPolicyMantenList(policyRef, phName, cardType, cardCode, uc);
}

	/**
	 * 质检查询
	 * @param session
	 * @return
	 * @throws IOException 
	 * @throws JsonMappingException 
	 * @throws JsonParseException 
	 */
	@RequestMapping(value="/getQualityList")
	@ResponseBody
	public List<Map<String,Object>> getQualityList(@RequestParam(value="jsonParam",required=false)String jsonParam,HttpSession session) throws JsonParseException, JsonMappingException, IOException{
		 String policyRef =null;
		 String phName = null;
		 String cardType = null;
		 String cardCode = null;
		 User user = (User) session.getAttribute("username");
		 long agentId = Long.parseLong(user.getAgentId());
		 if (jsonParam != null) {
			 @SuppressWarnings("unchecked")
			Map<String,Object> param=obMapper.readValue(jsonParam, Map.class);
			 //保单号
			  policyRef = (String) param.get("policyRef");
			  policyRef = "".equals(policyRef)?null:policyRef;
			  //投保人
			  phName = (String) param.get("phName");
			  phName = "".equals(phName)?null:phName;
			  //证件类型
			  cardType = (String) param.get("cardType");
			  cardType = "".equals(cardType)?null:cardType;
			  //证件号码
			  cardCode = (String) param.get("cardCode");
			 cardCode = "".equals(cardCode)?null:cardCode;
		}
		 if (cardType !=null && "National ID".equals(cardCode)) {
			 cardType =null;
		}
		 CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_ALLIANZ);
		return policyAgentsService.getQualityList(policyRef, phName, cardType, cardCode, agentId);
	}
	 
	 /**
	  * 获取照会PDF
	  * @param jsonParam
	  * @return
	  * @throws Exception
	  * URL:/pad_marketing/notes/getPDFUrl.action
	  */
	 @RequestMapping(value = "getPDFUrl")
	 @ResponseBody
	 public List<Map<String, Object>> getPDFUrl(@RequestParam(value = "jsonParam") String jsonParam) throws Exception {
		 CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_ALLIANZ);
		 @SuppressWarnings("unchecked")
		Map<String,Object> map = obMapper.readValue(jsonParam, Map.class);
		 try {
			return policyAgentsService.getPDFUrl(map);
		} catch (NumberFormatException e) {
			e.printStackTrace();
			throw new Exception("获取照会PDF失败,请检查您的网络");
		}
	 }
	 /**
	  * 新单回访模糊查询
	  * @param jsonParam
	  * @param session
	  * @return
	  * @throws JsonParseException
	  * @throws JsonMappingException
	  * @throws IOException
	  */
	 @RequestMapping(value="/getFuzzyPolicyReturnVisit")
	 @ResponseBody
	 public List<String> getFuzzyPolicyReturnVisit(@RequestParam(value="jsonParam")String jsonParam,HttpSession session) throws JsonParseException, JsonMappingException, IOException{
		 @SuppressWarnings("unchecked")
		Map<String,Object> param = obMapper.readValue(jsonParam, Map.class);
		 String phName = (String) param.get("phName");
		 if (StringUtils.isEmpty(phName)) {
			phName = null;
		}
		 User user=(User) session.getAttribute("username");
		 long agentId=Long.parseLong(user.getAgentId());
		 CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_ALLIANZ);
		 return  policyAgentsService.getFuzzySearch(agentId, phName);
	 }
	 /**
	  * 理赔模糊查询
	  * @param jsonParam
	  * @param session
	  * @return
	  * @throws JsonParseException
	  * @throws JsonMappingException
	  * @throws IOException
	  */
	 @RequestMapping(value="/getFuzzyClaimList")
	 @ResponseBody
	 public List<String> getFuzzyClaimList(@RequestParam(value="jsonParam")String jsonParam,HttpSession session) throws JsonParseException, JsonMappingException, IOException{
		 @SuppressWarnings("unchecked")
		Map<String,Object> param = obMapper.readValue(jsonParam, Map.class);
		 String phName = (String) param.get("phName");
		 if (StringUtils.isEmpty(phName)) {
			phName = null;
		}
		 User user=(User) session.getAttribute("username");
		 long agentId=Long.parseLong(user.getAgentId());
		 CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_ALLIANZ);
		 return  policyAgentsService.getFuzzySearch(agentId, phName);
	 }
	 /**
	  * 质检模糊查询
	  * @param jsonParam
	  * @param session
	  * @return
	  * @throws JsonParseException
	  * @throws JsonMappingException
	  * @throws IOException
	  */
	 @RequestMapping(value="/getFuzzyQualityList")
	 @ResponseBody
	 public List<String> getFuzzyQualityList(@RequestParam(value="jsonParam")String jsonParam,HttpSession session) throws JsonParseException, JsonMappingException, IOException{
		 @SuppressWarnings("unchecked")
		Map<String,Object> param = obMapper.readValue(jsonParam, Map.class);
		 String phName = (String) param.get("phName");
		 if (StringUtils.isEmpty(phName)) {
			phName = null;
		}
		 User user=(User) session.getAttribute("username");
		 long agentId=Long.parseLong(user.getAgentId());
		 CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_ALLIANZ);
		 return  policyAgentsService.getFuzzySearch(agentId, phName);
	 }
	 /**
	  * 保全模糊查询
	  * @param jsonParam
	  * @param session
	  * @return
	  * @throws JsonParseException
	  * @throws JsonMappingException
	  * @throws IOException
	  */
	 @RequestMapping(value="/getPolicyFuzzyMantenList")
	 @ResponseBody
	 public List<String> getPolicyFuzzyMantenList(@RequestParam(value="jsonParam")String jsonParam,HttpSession session) throws JsonParseException, JsonMappingException, IOException{
		 @SuppressWarnings("unchecked")
		Map<String,Object> param = obMapper.readValue(jsonParam, Map.class);
		 String phName = (String) param.get("phName");
		 if (StringUtils.isEmpty(phName)) {
			phName = null;
		}
		 User user=(User) session.getAttribute("username");
		 long agentId=Long.parseLong(user.getAgentId());
		 CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_ALLIANZ);
		 return  policyAgentsService.getFuzzySearch(agentId, phName);
	 }
}
